package com.ferry.registry.domain.registry;

import com.ferry.common.util.IpUtil;
import com.ferry.common.util.Pair;
import com.ferry.registry.domain.service.ServiceAddress;
import com.ferry.registry.domain.service.ServiceMetaData;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by daiyong
 * 服务地址和服务信息
 */
public class RegistryMetaData {

	private ServiceAddress serviceAddress;
	private ServiceMetaData serviceMetaData;
	private Object provider;
	private Map<String, List<Pair<Class<?>[], Class<?>[]>>> extensions = Maps.newHashMap();
	private volatile int connCount = 5;

	public RegistryMetaData() {
	}

	public RegistryMetaData(String group, String serviceName, String version, int port, Object provider, Map<String, List<Pair<Class<?>[], Class<?>[]>>> extensions) {
		this.serviceAddress = new ServiceAddress(IpUtil.getLocalAddress(), port);
		this.serviceMetaData = new ServiceMetaData(group, serviceName, version);
		this.provider = provider;
		this.extensions = extensions;
	}

	public RegistryMetaData(ServiceAddress serviceAddress, ServiceMetaData serviceMetaData) {
		this.serviceAddress = serviceAddress;
		this.serviceMetaData = serviceMetaData;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		RegistryMetaData that = (RegistryMetaData) o;
		return Objects.equals(serviceAddress, that.serviceAddress) &&
				Objects.equals(serviceMetaData, that.serviceMetaData);
	}

	@Override
	public int hashCode() {
		return Objects.hash(serviceAddress, serviceMetaData);
	}

	public ServiceAddress getServiceAddress() {
		return serviceAddress;
	}

	public void setServiceAddress(ServiceAddress serviceAddress) {
		this.serviceAddress = serviceAddress;
	}

	public ServiceMetaData getServiceMetaData() {
		return serviceMetaData;
	}

	public void setServiceMetaData(ServiceMetaData serviceMetaData) {
		this.serviceMetaData = serviceMetaData;
	}

	public Object getProvider() {
		return provider;
	}

	public void setProvider(Object provider) {
		this.provider = provider;
	}

	public Map<String, List<Pair<Class<?>[], Class<?>[]>>> getExtensions() {
		return extensions;
	}

	public void setExtensions(Map<String, List<Pair<Class<?>[], Class<?>[]>>> extensions) {
		this.extensions = extensions;
	}

	public int getConnCount() {
		return connCount;
	}

	public void setConnCount(int connCount) {
		this.connCount = connCount;
	}

	@Override
	public String toString() {
		return "RegistryMetaData{" +
				"serviceAddress=" + serviceAddress +
				", serviceMetaData=" + serviceMetaData +
				'}';
	}

}
